package com.xyoto.community.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import com.xyoto.community.config.property.XyotoMainConfigProperties;
import com.xyoto.community.utils.handler.MyBatisHandle;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.xyoto.community.mapper.main", sqlSessionFactoryRef = "xyotoMainSqlSessionFactory")
@Slf4j
@Import({XyotoMainConfigProperties.class})
public class XyotoMainConfig {

    @Autowired
    private MyBatisHandle myBatisHandle;

    @Autowired
    private XyotoMainConfigProperties xyotoMainConfigProperties;

    @Bean(name = "xyotoMainDataSource")
    public DataSource xyotoMainDataSource(){
        log.info("开始初始化【xyoto_main】数据源");
        // 新建一个支持XA协议的数据源
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        // 设置数据库信息
        mysqlXADataSource.setURL(xyotoMainConfigProperties.getUrl());
        mysqlXADataSource.setUser(xyotoMainConfigProperties.getUsername());
        mysqlXADataSource.setPassword(xyotoMainConfigProperties.getPassword());
        // 把创建好的数据源对象交给atomikos管理
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        // 设置资源标识
        atomikosDataSourceBean.setUniqueResourceName(xyotoMainConfigProperties.getUniqueResourceName());
        log.info("完成初始化【xyoto_main】数据源");
        return atomikosDataSourceBean;
    }

    @Bean(name = "xyotoMainSqlSessionFactory")
    public SqlSessionFactory xyotoMainSqlSessionFactory(@Qualifier("xyotoMainDataSource") DataSource dataSource) throws Exception {

        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/main/**/**/*.xml")
        );
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        bean.setConfiguration(configuration);
        bean.setPlugins(myBatisHandle);

        return bean.getObject();
    }

    @Bean(name = "xyotoMainSqlSessionTemplate")
    public SqlSessionTemplate xyotoMainSqlSessionTemplate(@Qualifier("xyotoMainSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
